package com.dream.view;

import com.dream.main.DreamGameActivity;
import com.dream.tool.Draw;
import com.dream.tool.Tools;
import com.dream.tool.Touch;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class LoadView extends SurfaceView implements Callback {

	private DreamGameActivity _activity;
	private TutorialThread _thread;
	Bitmap bg_load,bg_pause;
	int i,Bm_Alpha;
	boolean sendmesg;
	Draw draw;
	Touch touch;

	public LoadView(DreamGameActivity _activity) {
		super(_activity);
		this._activity = _activity;
		getHolder().addCallback(this);
		initBitmap();
		Bm_Alpha=255;
		i=0;
		draw=new Draw();
		touch=new Touch();
	}

	private void initBitmap() {
		bg_load = Tools.getBitmap(_activity, Tools.bg_load);
		bg_pause=Tools.getBitmap(_activity, Tools.bg_pause);
	}

	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		draw.BitmapDraw(canvas, bg_pause, 430, 10);
		draw.setPaintColor(Color.WHITE);
		draw.setPaintSize(15);
		draw.TextLine(canvas, 425, 65, 480, 65);
		if (i<Tools.Loadstr.length) {
			draw.BitmapDraw(canvas, bg_load, 0, 0);
			draw.TextDraw(canvas, Tools.Loadstr[i], Tools.screenWidth/15+5, 317);
		}else {
			draw.setPaintAlpha(Bm_Alpha);
			draw.BitmapDraw(canvas, bg_load, 0, 0,"Al");
		}
		if (Bm_Alpha==0) {
//			Paint paint1=new Paint();
//			paint1.setColor(Color.WHITE);
//			paint1.setTextSize(15);
//			canvas.drawText(Tools.Loadstrlast, Tools.screenWidth/15+5, 317, paint1);
			Draw draw1=new Draw();
			draw1.setPaintColor(Color.WHITE);
			draw1.setPaintSize(15);
			draw1.TextDraw(canvas, Tools.Loadstrlast, Tools.screenWidth/15+5, 317);
			sendmesg=true;
		}			
	}
	public void Logic(long time) {
		if (i>=Tools.Loadstr.length) {
			if (Bm_Alpha>0) {
			Bm_Alpha-=10;
		}else {
			Bm_Alpha=0;
		}
		}
		

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if (i<Tools.Loadstr.length) {
				i++;
			}
		}
		if(event.getAction() == MotionEvent.ACTION_UP){
			if (sendmesg==true) {
				_activity.handler.sendEmptyMessage(2);
			}
		}
		return true;
	}

	@Override
	public void surfaceCreated(SurfaceHolder sfh) {
		_thread = new TutorialThread(sfh, this);
		_thread.setFlag(true);
		_thread.start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		_thread.setFlag(false);
		while (retry) {
			try {
				_thread.join();
				retry = false;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	class TutorialThread extends Thread {

		SurfaceHolder surfaceHolder;
		LoadView menuview;
		long time = 0;
		Canvas canvas;

		public TutorialThread(SurfaceHolder surfaceHolder, LoadView menuview) {
			this.surfaceHolder = surfaceHolder;
			this.menuview = menuview;
		}

		boolean _isFlag;

		public void setFlag(boolean _isFlag) {
			this._isFlag = _isFlag;
		}

		@Override
		public void run() {
			while (_isFlag) {
				time++;
				try {
					canvas = surfaceHolder.lockCanvas();
					synchronized (surfaceHolder) {
						onDraw(canvas);
						Logic(time);
					}
				} finally {
					if (canvas != null)
						surfaceHolder.unlockCanvasAndPost(canvas);
				}
				try {
					if (time == 1) {
						Thread.sleep(1000);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
